package dfcf

import (
	"encoding/json"
	"fmt"
	"gitee.com/quant1x/engine/utils"
	"gitee.com/quant1x/exchange"
	"gitee.com/quant1x/gox/api"
	"gitee.com/quant1x/gox/http"
	urlpkg "net/url"
	"strings"
)

// 资金流向
// 数据来源: https://data.eastmoney.com/zjlx/603260.html
// 数据接口: https://push2his.eastmoney.com/api/qt/stock/fflow/daykline/get?cb=jQuery112309979842415798676_1686522654839&lmt=0&klt=101&fields1=f1%2Cf2%2Cf3%2Cf7&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61%2Cf62%2Cf63%2Cf64%2Cf65&ut=b2884a393a59ad64002292a3e90d46a5&secid=1.603260&_=1686522654840
// 样本数据:
// {
//    "rc":0,
//    "rt":22,
//    "svr":2887176469,
//    "lt":1,
//    "full":0,
//    "dlmkts":"",
//    "data":{
//        "code":"603260",
//        "market":1,
//        "name":"合盛硅业",
//        "klines":[
//            "2023-01-09,-92087164.0,29280676.0,62806480.0,-12290380.0,-79796784.0,-10.00,3.18,6.82,-1.34,-8.67,87.72,0.31,0.00,0.00",
//            "2023-01-10,-31347094.0,58376511.0,-27029417.0,-9794039.0,-21553055.0,-7.53,14.03,-6.49,-2.35,-5.18,86.65,-1.22,0.00,0.00",
//            "2023-01-11,-66303996.0,29572185.0,36731811.0,-41386624.0,-24917372.0,-18.51,8.26,10.26,-11.56,-6.96,84.99,-1.92,0.00,0.00",
//            "2023-01-12,-36066283.0,7936405.0,28129877.0,-12624768.0,-23441515.0,-10.85,2.39,8.46,-3.80,-7.05,84.80,-0.22,0.00,0.00",
//            "2023-01-13,-13516226.0,4875138.0,8641088.0,-6003237.0,-7512989.0,-3.66,1.32,2.34,-1.63,-2.04,84.28,-0.61,0.00,0.00",
//            "2023-01-16,7263948.0,-17890309.0,10626362.0,-13295402.0,20559350.0,1.29,-3.17,1.88,-2.36,3.64,86.86,3.06,0.00,0.00",
//            "2023-01-17,-16063087.0,-2379327.0,18442413.0,-6571109.0,-9491978.0,-4.69,-0.69,5.39,-1.92,-2.77,87.09,0.26,0.00,0.00",
//            "2023-01-18,42605374.0,-42913367.0,308000.0,11714691.0,30890683.0,5.05,-5.09,0.04,1.39,3.66,89.12,2.33,0.00,0.00",
//            "2023-01-19,-1206719.0,-20161550.0,21368269.0,13847315.0,-15054034.0,-0.35,-5.90,6.25,4.05,-4.40,88.22,-1.01,0.00,0.00",
//            "2023-01-20,51557054.0,-37151331.0,-14405724.0,18406646.0,33150408.0,11.75,-8.47,-3.28,4.20,7.56,89.72,1.70,0.00,0.00",
//            "2023-01-30,43524920.0,-39961170.0,-3563744.0,6624109.0,36900811.0,5.93,-5.44,-0.49,0.90,5.03,92.21,2.78,0.00,0.00",
//            "2023-01-31,13053017.0,52695.0,-13105712.0,-281512.0,13334529.0,2.77,0.01,-2.78,-0.06,2.83,92.11,-0.11,0.00,0.00",
//            "2023-02-01,24617057.0,-2733736.0,-21883319.0,647537.0,23969520.0,3.20,-0.36,-2.85,0.08,3.12,95.32,3.48,0.00,0.00",
//            "2023-02-02,-34726944.0,-12080775.0,46807718.0,-40755931.0,6028987.0,-6.41,-2.23,8.65,-7.53,1.11,94.50,-0.86,0.00,0.00",
//            "2023-02-03,-37150492.0,15564110.0,21586382.0,-23809298.0,-13341194.0,-6.50,2.72,3.78,-4.17,-2.33,92.36,-2.26,0.00,0.00",
//            "2023-02-06,-19789022.0,-19478758.0,39267780.0,-15232543.0,-4556479.0,-4.76,-4.69,9.45,-3.66,-1.10,93.08,0.78,0.00,0.00",
//            "2023-02-07,-15541119.0,4067158.0,11473961.0,3115404.0,-18656523.0,-4.53,1.18,3.34,0.91,-5.43,91.88,-1.29,0.00,0.00",
//            "2023-02-08,-1603646.0,-1571431.0,3175077.0,-6045553.0,4441907.0,-0.76,-0.75,1.51,-2.87,2.11,91.84,-0.04,0.00,0.00",
//            "2023-02-09,51489241.0,-36594729.0,-14894512.0,62733728.0,-11244487.0,4.96,-3.53,-1.43,6.04,-1.08,97.18,5.81,0.00,0.00",
//            "2023-02-10,-32534576.0,-31827307.0,64361884.0,-21791264.0,-10743312.0,-4.16,-4.07,8.24,-2.79,-1.37,96.92,-0.27,0.00,0.00",
//            "2023-02-13,-38815338.0,37205196.0,1610142.0,-31554722.0,-7260616.0,-9.41,9.02,0.39,-7.65,-1.76,95.81,-1.15,0.00,0.00",
//            "2023-02-14,-42986515.0,-3378548.0,46365062.0,-57853929.0,14867414.0,-9.86,-0.77,10.63,-13.26,3.41,96.70,0.93,0.00,0.00",
//            "2023-02-15,-24945110.0,23130481.0,1814629.0,-11997680.0,-12947430.0,-6.90,6.40,0.50,-3.32,-3.58,95.22,-1.53,0.00,0.00",
//            "2023-02-16,-69819364.0,56306298.0,13513067.0,-49475335.0,-20344029.0,-14.53,11.72,2.81,-10.29,-4.23,92.75,-2.59,0.00,0.00",
//            "2023-02-17,-22449608.0,20788980.0,1660628.0,-15594503.0,-6855105.0,-9.21,8.53,0.68,-6.40,-2.81,91.20,-1.67,0.00,0.00",
//            "2023-02-20,-18928109.0,10774240.0,8153869.0,-580659.0,-18347450.0,-4.68,2.67,2.02,-0.14,-4.54,93.12,2.11,0.00,0.00",
//            "2023-02-21,-30415860.0,-4188253.0,34604112.0,-10803470.0,-19612390.0,-3.16,-0.44,3.60,-1.12,-2.04,97.02,4.19,0.00,0.00",
//            "2023-02-22,-62449312.0,74500442.0,-12051129.0,-39207757.0,-23241555.0,-14.18,16.92,-2.74,-8.90,-5.28,97.20,0.19,0.00,0.00",
//            "2023-02-23,-13423407.0,27492687.0,-14069279.0,-13402552.0,-20855.0,-3.71,7.60,-3.89,-3.70,-0.01,95.75,-1.49,0.00,0.00",
//            "2023-02-24,-51907930.0,23632127.0,28275804.0,-26378407.0,-25529523.0,-20.97,9.55,11.42,-10.66,-10.31,94.26,-1.56,0.00,0.00",
//            "2023-02-27,30754712.0,-33441149.0,2686438.0,27418010.0,3336702.0,8.41,-9.14,0.73,7.50,0.91,96.17,2.03,0.00,0.00",
//            "2023-02-28,-15073512.0,-19967341.0,35040852.0,-8482306.0,-6591206.0,-4.26,-5.64,9.89,-2.39,-1.86,97.11,0.98,0.00,0.00",
//            "2023-03-01,-51521580.0,-3051319.0,54572898.0,-51491604.0,-29976.0,-19.84,-1.18,21.02,-19.83,-0.01,95.93,-1.22,0.00,0.00",
//            "2023-03-02,-108218400.0,56395709.0,51822692.0,-31096407.0,-77121993.0,-25.19,13.13,12.06,-7.24,-17.95,93.13,-2.92,0.00,0.00",
//            "2023-03-03,-21002420.0,997451.0,20004968.0,-8518650.0,-12483770.0,-7.22,0.34,6.88,-2.93,-4.29,92.32,-0.87,0.00,0.00",
//            "2023-03-06,-40537174.0,-3793851.0,44331026.0,-29530442.0,-11006732.0,-12.18,-1.14,13.32,-8.88,-3.31,92.91,0.64,0.00,0.00",
//            "2023-03-07,-2935041.0,-25959191.0,28894233.0,3421132.0,-6356173.0,-0.71,-6.24,6.95,0.82,-1.53,92.28,-0.68,0.00,0.00",
//            "2023-03-08,-19340544.0,21536026.0,-2195481.0,-10592195.0,-8748349.0,-7.38,8.22,-0.84,-4.04,-3.34,91.62,-0.72,0.00,0.00",
//            "2023-03-09,-16474453.0,12896563.0,3577891.0,-9841105.0,-6633348.0,-6.42,5.03,1.39,-3.83,-2.58,89.73,-2.06,0.00,0.00",
//            "2023-03-10,-53310992.0,29347785.0,23963208.0,-28041086.0,-25269906.0,-15.63,8.61,7.03,-8.22,-7.41,87.54,-2.44,0.00,0.00",
//            "2023-03-13,-16183318.0,19867045.0,-3683726.0,-10838742.0,-5344576.0,-7.81,9.59,-1.78,-5.23,-2.58,88.14,0.69,0.00,0.00",
//            "2023-03-14,-25914629.0,15919820.0,9994810.0,-8528240.0,-17386389.0,-6.40,3.93,2.47,-2.11,-4.29,85.10,-3.45,0.00,0.00",
//            "2023-03-15,-8197490.0,28202718.0,-20005227.0,-8012085.0,-185405.0,-3.00,10.33,-7.32,-2.93,-0.07,84.15,-1.12,0.00,0.00",
//            "2023-03-16,-37783623.0,50642134.0,-12858510.0,-24938422.0,-12845201.0,-14.08,18.87,-4.79,-9.29,-4.79,82.04,-2.51,0.00,0.00",
//            "2023-03-17,-6541496.0,7496669.0,-955174.0,-4141863.0,-2399633.0,-2.64,3.03,-0.39,-1.67,-0.97,81.62,-0.51,0.00,0.00",
//            "2023-03-20,1044702.0,-13937783.0,12893082.0,-10711113.0,11755815.0,0.46,-6.20,5.73,-4.76,5.23,82.38,0.93,0.00,0.00",
//            "2023-03-21,2617784.0,-44323638.0,41705855.0,-13492579.0,16110363.0,0.69,-11.73,11.04,-3.57,4.26,82.28,-0.12,0.00,0.00",
//            "2023-03-22,1559358.0,5039161.0,-6598518.0,2052929.0,-493571.0,0.61,1.99,-2.60,0.81,-0.19,82.11,-0.21,0.00,0.00",
//            "2023-03-23,-3905909.0,-14869990.0,18775900.0,2163209.0,-6069118.0,-1.92,-7.32,9.24,1.06,-2.99,81.82,-0.35,0.00,0.00",
//            "2023-03-24,13276400.0,-23105004.0,9828603.0,10957898.0,2318502.0,4.95,-8.62,3.67,4.09,0.87,82.23,0.50,0.00,0.00",
//            "2023-03-27,26877988.0,-22332313.0,-4545674.0,12811288.0,14066700.0,9.26,-7.69,-1.57,4.41,4.85,82.65,0.51,0.00,0.00",
//            "2023-03-28,35854059.0,-29863101.0,-5990957.0,16969457.0,18884602.0,7.49,-6.24,-1.25,3.54,3.94,85.17,3.05,0.00,0.00",
//            "2023-03-29,-17932302.0,36631375.0,-18699072.0,-16322014.0,-1610288.0,-4.61,9.41,-4.80,-4.19,-0.41,83.37,-2.11,0.00,0.00",
//            "2023-03-30,-18669823.0,18505449.0,164374.0,-5024219.0,-13645604.0,-6.99,6.93,0.06,-1.88,-5.11,82.52,-1.02,0.00,0.00",
//            "2023-03-31,-16265083.0,24415426.0,-8150342.0,-19239816.0,2974733.0,-5.83,8.75,-2.92,-6.90,1.07,82.13,-0.47,0.00,0.00",
//            "2023-04-03,-14375240.0,15164510.0,-789269.0,-9931134.0,-4444106.0,-3.37,3.56,-0.19,-2.33,-1.04,83.51,1.68,0.00,0.00",
//            "2023-04-04,-92315457.0,56002222.0,36313234.0,-65583716.0,-26731741.0,-22.83,13.85,8.98,-16.22,-6.61,80.94,-3.08,0.00,0.00",
//            "2023-04-06,-19697039.0,25799484.0,-6102445.0,-12662211.0,-7034828.0,-7.56,9.90,-2.34,-4.86,-2.70,81.31,0.46,0.00,0.00",
//            "2023-04-07,-3817607.0,2902820.0,914787.0,3209396.0,-7027003.0,-2.06,1.57,0.49,1.73,-3.79,81.50,0.23,0.00,0.00",
//            "2023-04-10,-97208799.0,53227379.0,43981421.0,-41854999.0,-55353800.0,-25.52,13.98,11.55,-10.99,-14.53,80.23,-1.56,0.00,0.00",
//            "2023-04-11,-399065.0,5214275.0,-4815208.0,5807785.0,-6206850.0,-0.18,2.30,-2.12,2.56,-2.74,80.42,0.24,0.00,0.00",
//            "2023-04-12,-4830228.0,1282245.0,3547983.0,-10283769.0,5453541.0,-1.97,0.52,1.45,-4.20,2.23,80.52,0.12,0.00,0.00",
//            "2023-04-13,-16893674.0,10654229.0,6239447.0,-12027387.0,-4866287.0,-8.13,5.13,3.00,-5.79,-2.34,79.33,-1.48,0.00,0.00",
//            "2023-04-14,19219157.0,-5519375.0,-13699782.0,19136658.0,82499.0,5.81,-1.67,-4.14,5.79,0.02,80.79,1.84,0.00,0.00",
//            "2023-04-17,-424014.0,293721.0,130293.0,5296307.0,-5720321.0,-0.14,0.10,0.04,1.77,-1.92,81.84,1.30,0.00,0.00",
//            "2023-04-18,14016521.0,-3343665.0,-10672856.0,15746714.0,-1730193.0,4.13,-0.98,-3.14,4.63,-0.51,81.69,-0.18,0.00,0.00",
//            "2023-04-19,-34882480.0,25814996.0,9067484.0,-20637183.0,-14245297.0,-11.74,8.69,3.05,-6.94,-4.79,79.99,-2.08,0.00,0.00",
//            "2023-04-20,-17543810.0,15686011.0,1857799.0,-20336873.0,2793063.0,-7.56,6.76,0.80,-8.76,1.20,79.43,-0.70,0.00,0.00",
//            "2023-04-21,-12109512.0,11966654.0,142857.0,2089947.0,-14199459.0,-5.07,5.01,0.06,0.87,-5.94,78.02,-1.78,0.00,0.00",
//            "2023-04-24,-69444460.0,61552108.0,7892351.0,-33308989.0,-36135471.0,-18.33,16.25,2.08,-8.79,-9.54,72.97,-6.47,0.00,0.00",
//            "2023-04-25,-63889764.0,52697710.0,11192054.0,-27949134.0,-35940630.0,-21.45,17.69,3.76,-9.38,-12.06,70.51,-3.37,0.00,0.00",
//            "2023-04-26,1029628.0,2343198.0,-3372826.0,8485819.0,-7456191.0,0.32,0.72,-1.04,2.61,-2.29,73.64,4.44,0.00,0.00",
//            "2023-04-27,-20443348.0,17801399.0,2641949.0,-22779035.0,2335687.0,-9.81,8.54,1.27,-10.93,1.12,72.48,-1.58,0.00,0.00",
//            "2023-04-28,-8466692.0,15139040.0,-6672348.0,-10665566.0,2198874.0,-5.69,10.18,-4.49,-7.17,1.48,72.35,-0.18,0.00,0.00",
//            "2023-05-04,-5605813.0,1022232.0,4583581.0,-1939023.0,-3666790.0,-3.97,0.72,3.25,-1.37,-2.60,72.01,-0.47,0.00,0.00",
//            "2023-05-05,3705270.0,14904084.0,-18609353.0,1187923.0,2517347.0,2.25,9.06,-11.31,0.72,1.53,70.79,-1.69,0.00,0.00",
//            "2023-05-08,-3883489.0,-8049405.0,11932894.0,-6614775.0,2731286.0,-2.10,-4.36,6.46,-3.58,1.48,71.20,0.58,0.00,0.00",
//            "2023-05-09,108834.0,-405876.0,297043.0,7015429.0,-6906595.0,0.05,-0.19,0.14,3.27,-3.22,70.35,-1.19,0.00,0.00",
//            "2023-05-10,-1884388.0,18915558.0,-17031170.0,15947850.0,-17832238.0,-0.90,9.03,-8.13,7.62,-8.52,70.62,0.38,0.00,0.00",
//            "2023-05-11,5755150.0,-2617901.0,-3137249.0,5011172.0,743978.0,2.66,-1.21,-1.45,2.32,0.34,71.52,1.27,0.00,0.00",
//            "2023-05-12,-22436530.0,30143532.0,-7707000.0,-15211473.0,-7225057.0,-8.20,11.01,-2.82,-5.56,-2.64,69.37,-3.01,0.00,0.00",
//            "2023-05-15,-6930730.0,23714083.0,-16783353.0,-889472.0,-6041258.0,-3.12,10.67,-7.55,-0.40,-2.72,70.43,1.53,0.00,0.00",
//            "2023-05-16,24999805.0,-19750255.0,-5249550.0,16232501.0,8767304.0,11.14,-8.80,-2.34,7.24,3.91,71.78,1.92,0.00,0.00",
//            "2023-05-17,-16753225.0,21868716.0,-5115489.0,-2824894.0,-13928331.0,-7.59,9.90,-2.32,-1.28,-6.31,70.12,-2.31,0.00,0.00",
//            "2023-05-18,-17164925.0,34164695.0,-16999769.0,-14000442.0,-3164483.0,-9.13,18.16,-9.04,-7.44,-1.68,69.25,-1.24,0.00,0.00",
//            "2023-05-19,-1551355.0,1258142.0,293213.0,1287934.0,-2839289.0,-1.30,1.06,0.25,1.08,-2.38,69.77,0.75,0.00,0.00",
//            "2023-05-22,-1836483.0,-1009003.0,2845486.0,4035513.0,-5871996.0,-0.83,-0.46,1.28,1.82,-2.65,71.36,2.28,0.00,0.00",
//            "2023-05-23,-4237195.0,-797703.0,5034899.0,-3307188.0,-930007.0,-2.21,-0.42,2.63,-1.73,-0.49,70.93,-0.60,0.00,0.00",
//            "2023-05-24,-7778951.0,2673317.0,5105635.0,993659.0,-8772610.0,-5.05,1.74,3.32,0.65,-5.70,70.95,0.03,0.00,0.00",
//            "2023-05-25,5122342.0,-4829770.0,-292572.0,-67729.0,5190071.0,2.77,-2.61,-0.16,-0.04,2.80,70.23,-1.01,0.00,0.00",
//            "2023-05-26,-19526861.0,15825529.0,3701333.0,-17136358.0,-2390503.0,-10.80,8.76,2.05,-9.48,-1.32,68.87,-1.94,0.00,0.00",
//            "2023-05-29,-18536590.0,16329707.0,2206884.0,-17633967.0,-902623.0,-9.88,8.70,1.18,-9.40,-0.48,67.53,-1.95,0.00,0.00",
//            "2023-05-30,-17064901.0,27975372.0,-10910470.0,-16064764.0,-1000137.0,-9.68,15.87,-6.19,-9.11,-0.57,67.80,0.40,0.00,0.00",
//            "2023-05-31,-22480606.0,24368999.0,-1888392.0,-9114695.0,-13365911.0,-12.13,13.15,-1.02,-4.92,-7.21,67.36,-0.65,0.00,0.00",
//            "2023-06-01,10533602.0,-2013809.0,-8519793.0,6054261.0,4479341.0,5.43,-1.04,-4.39,3.12,2.31,68.19,1.23,0.00,0.00",
//            "2023-06-02,1200479.0,-4269641.0,3069162.0,-2518255.0,3718734.0,0.27,-0.98,0.70,-0.58,0.85,71.80,5.29,0.00,0.00",
//            "2023-06-05,-18738993.0,15808892.0,2930101.0,-3575916.0,-15163077.0,-10.06,8.49,1.57,-1.92,-8.14,70.59,-1.69,0.00,0.00",
//            "2023-06-06,-23760222.0,19245566.0,4514657.0,-14615218.0,-9145004.0,-11.80,9.56,2.24,-7.26,-4.54,68.92,-2.37,0.00,0.00",
//            "2023-06-07,-20472470.0,15882312.0,4590159.0,-2188936.0,-18283534.0,-10.93,8.48,2.45,-1.17,-9.76,67.82,-1.60,0.00,0.00",
//            "2023-06-08,-4211390.0,5087159.0,-875768.0,-9910901.0,5699511.0,-1.35,1.63,-0.28,-3.18,1.83,69.99,3.20,0.00,0.00",
//            "2023-06-09,-1910466.0,-10981872.0,12892337.0,-11467381.0,9556915.0,-0.34,-1.93,2.26,-2.01,1.68,68.95,-1.49,0.00,0.00"
//        ]
//    }
//}

const (
	urlFundFlow = "https://push2his.eastmoney.com/api/qt/stock/fflow/daykline/get"
)

type rawFundFlow struct {
	Rc     int    `json:"rc"`
	Rt     int    `json:"rt"`
	Svr    int    `json:"svr"`
	Lt     int    `json:"lt"`
	Full   int    `json:"full"`
	Dlmkts string `json:"dlmkts"`
	Data   struct {
		Code   string   `json:"code"`
		Market int      `json:"market"`
		Name   string   `json:"name"`
		Klines []string `json:"klines"`
	} `json:"data"`
}

// FundFlow 资金流向
type FundFlow struct {
	Date            string `array:"0"`
	Code            string
	Main            float64 `array:"1"`
	MainRatio       float64 `array:"6"`
	SuperLarge      float64 `array:"5"`
	SuperLargeRatio float64 `array:"10"`
	Large           float64 `array:"4"`
	LargeRatio      float64 `array:"9"`
	Medium          float64 `array:"3"`
	MediumRatio     float64 `array:"8"`
	Small           float64 `array:"2"`
	SmallRatio      float64 `array:"7"`
}

// IndividualStocksFundFlow 个股资金流向
func IndividualStocksFundFlow(securityCode, date string) (list []FundFlow) {
	beginDate := ""
	if len(date) > 0 {
		beginDate = exchange.FixTradeDate(date)
	}

	mId, _, code := exchange.DetectMarket(securityCode)
	params := urlpkg.Values{
		"lmt":     {"0"},
		"klt":     {"101"},
		"secid":   {fmt.Sprint(mId, ".", code)},
		"fields1": {"f1,f2,f3,f7"},
		"fields2": {"f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63,f64,f65"},
		"ut":      {"b2884a393a59ad64002292a3e90d46a5"},
		"_":       {fmt.Sprintf("%d", utils.Timestamp())},
	}
	url := urlFundFlow + "?" + params.Encode()
	data, err := http.Get(url)
	if err != nil {
		return
	}
	//fmt.Println(api.Bytes2String(data))
	var raw rawFundFlow
	err = json.Unmarshal(data, &raw)
	if err != nil {
		return
	}
	lines := raw.Data.Klines
	if len(lines) == 0 {
		return
	}
	for _, line := range lines {
		array := strings.Split(line, ",")
		var ff FundFlow
		err := api.Convert(array, &ff)
		if err != nil {
			continue
		}
		if ff.Date < beginDate {
			continue
		}
		ff.Code = securityCode
		list = append(list, ff)
	}
	return
}
